package util;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Atom {

    private double ATOMIC_WEIGHT;
    private String symbol;


    public Node current;

    public double getAtomicWeight() {
        if (ATOMIC_WEIGHT < -998)
            ATOMIC_WEIGHT = Double.parseDouble(extract("ATOMIC_WEIGHT"));
        return ATOMIC_WEIGHT;
    }

    public String getSymbol() {
        return symbol;
    }

    /**
     * Constructs a new Atom based on the node. Note that this method should never be called.
     *
     * @param fstNode The Node which contains the info on the element.
     */
    public Atom(Node fstNode) {
        current = fstNode;
        ATOMIC_WEIGHT = -999;
        symbol = extract("SYMBOL");

    }

    private String extract(String tag) {
        try {
            Element a = (Element) current;
            NodeList b = a.getElementsByTagName(tag);
            a = (Element) b.item(0);
            b = a.getChildNodes();
            return b.item(0).getNodeValue();
        } catch (NullPointerException e) {
            return "-1";
        }
    }

    public int hashCode() {
        return symbol.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        return (o instanceof Atom) && ((Atom) o).getSymbol().equals(symbol);
    }

    @Override
    public String toString() {
        return symbol;
    }

}
